package com.qys.livesMall.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qys.livesMall.shop.modal.entity.ShopGoodsSku;
import com.qys.livesMall.shop.mapper.ShopGoodsSkuMapper;
import com.qys.livesMall.shop.modal.entity.ShopGoodsSkuStocks;
import com.qys.livesMall.shop.service.ShopGoodsSkuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qys.livesMall.shop.service.ShopGoodsSkuStocksService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * sku库存 服务实现类
 * </p>
 *
 * @author gmx
 * @since 2023-06-07
 */
@Service
public class ShopGoodsSkuServiceImpl extends ServiceImpl<ShopGoodsSkuMapper, ShopGoodsSku> implements ShopGoodsSkuService {
    @Resource
    private ShopGoodsSkuStocksService stocksService;

    @Autowired
    private ShopGoodsSkuMapper shopGoodsSkuMapper;

    @Override
    public Long countSkuNum(Long shopId) {
        List<ShopGoodsSku> skus = queryByShopId(shopId);
        if (ObjectUtils.isEmpty(queryByShopId(shopId))){
            return 0L;
        }
        List<Long> skuIds = skus.stream().map(ShopGoodsSku::getId).collect(Collectors.toList());
        LambdaQueryWrapper<ShopGoodsSkuStocks> stocksLambdaQueryWrapper = new LambdaQueryWrapper<>();
        stocksLambdaQueryWrapper.in(ShopGoodsSkuStocks::getSkuId,skuIds);
        List<ShopGoodsSkuStocks> stockList = stocksService.list(stocksLambdaQueryWrapper);
        int num = stockList.stream().mapToInt(ShopGoodsSkuStocks::getNum).sum();
        return Long.valueOf(num);
    }

    @Override
    public List<ShopGoodsSku> queryByShopId(Long shopId) {
        LambdaQueryWrapper<ShopGoodsSku> skuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        skuLambdaQueryWrapper.eq(ShopGoodsSku::getShopId,shopId);
        return super.list(skuLambdaQueryWrapper);
    }

    @Override
    public ShopGoodsSku getGoodsSku(String skuId, Long shopId) {
        LambdaQueryWrapper<ShopGoodsSku> skuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        skuLambdaQueryWrapper.eq(ShopGoodsSku::getShopId,shopId);
        skuLambdaQueryWrapper.eq(ShopGoodsSku::getSkuId,skuId);
        skuLambdaQueryWrapper.last("limit 1");
        return getOne(skuLambdaQueryWrapper);
    }

    @Override
    public List<ShopGoodsSku> getAllSku(List<Long> shopIds){
        LambdaQueryWrapper<ShopGoodsSku> qw = new LambdaQueryWrapper<>();
        qw.select(ShopGoodsSku::getId,ShopGoodsSku::getShopId,ShopGoodsSku::getAvailableNum,ShopGoodsSku::getPrice);
        qw.in(ShopGoodsSku::getShopId,shopIds);
        qw.ge(ShopGoodsSku::getAvailableNum,0);
        qw.orderByAsc(ShopGoodsSku::getPrice);
        return super.list(qw);
    }
    @Override
    public List<ShopGoodsSku> getAllSkuByGoodsInfoId(List<Long> shopGoodsInfoIds, BigDecimal amount){
        if(CollectionUtils.isNotEmpty(shopGoodsInfoIds)){
            LambdaQueryWrapper<ShopGoodsSku> qw = new LambdaQueryWrapper<>();
            qw.select(ShopGoodsSku::getId,ShopGoodsSku::getShopId,ShopGoodsSku::getAvailableNum,ShopGoodsSku::getPrice);
            qw.in(ShopGoodsSku::getShopGoodsId,shopGoodsInfoIds);
            qw.ge(ShopGoodsSku::getAvailableNum,0);
            qw.le(ShopGoodsSku::getPrice,amount);
            qw.orderByAsc(ShopGoodsSku::getPrice);
            return super.list(qw);
        }
        return new ArrayList<>();
    }

    @Override

    public List<ShopGoodsSku> getBySku(List<Long> shopSkuIds) {
        return super.listByIds(shopSkuIds);
    }

    @Override
    public BigDecimal getPostage(List<Long> skuIds) {
        return shopGoodsSkuMapper.getPostage(skuIds);
    }

    @Override
    public List<ShopGoodsSku> getRandSkus(Long shopId, int limit) {
        return shopGoodsSkuMapper.getRandSkus(shopId,limit);
    }

    @Override
    public List<Long> getLivesSessionForSku(Long skuId, List<String> liveIds) {
        return shopGoodsSkuMapper.getLivesSessionForSku(skuId, liveIds);
    }

    @Override
    public List<Long> getSkuIdsByGoodsId(Long shopGoodsId) {
        LambdaQueryWrapper<ShopGoodsSku> qw = new LambdaQueryWrapper<>();
        qw.select(ShopGoodsSku::getId);
        qw.eq(ShopGoodsSku::getShopGoodsId,shopGoodsId);
        return list(qw).stream().map(ShopGoodsSku::getId).collect(Collectors.toList());
    }

    @Override
    public ShopGoodsSku getSkuByGoods( List<Long> shopGoodsIds,BigDecimal amount, Long shopId){
        return shopGoodsSkuMapper.getSkuByGoods(shopGoodsIds,amount,shopId);
    }
}
